<?php
FLEA::loadClass('FLEA_Db_TableDataGateway');

class	Model_Copyright	extends		FLEA_Db_TableDataGateway
{
	var		$tableName		=	'copyright';
	var		$primaryKey		=	'CopyrightID';
	
	function saveCopy($copy)
	{
		$copyTem	=	$this->findByField('ClientID',$copy['ClientID']);
		if($copyTem['ClientID'] == $copy['ClientID'])
		{
			$copy['CopyRightID']	=	$copyTem['CopyRightID'];
			return $this->save($copy);
		}
		else
		{
			return $this->create($copy);
		}
	}
	
	function findAllCopyrights($userID,$role)
	{
		$sql	=	"
			select distinct copyright.*,client.Business as ClientName,firm.Name as FirmName,
				inventorUser.Email as InventorEmail,inventorUser.FirstName as InventorFirstName,inventorUser.LastName as InventorLastName,
				practitionerUser.Email as PractitionerEmail,practitionerUser.FirstName as PractititonerFirstName,
				practitionerUser.LastName as PractitionerLastName
				
				 from 
			
				copyright
							
				left join client on copyright.ClientID=client.ClientID 
							
				left join firm on copyright.FirmID=firm.FirmID
							
				left join inventor on copyright.InventorID=inventor.InventorID
							
				left join practitioner on copyright.PractitionerID=practitioner.PractitionerID
				
				left join user as inventorUser on inventorUser.UserID=inventor.UserID
				
				left join user as practitionerUser on practitionerUser.UserID=practitioner.UserID
		";

		switch($role)
		{
			case 1:
				$sql	.=	' where copyright.ClientID='.$userID;
				break;
			case 2:
				$sql	.=	' where copyright.InventorID='.$userID;
				break;
			case 3:
				$sql	.=	" left join bid  on copyright.CopyrightID=bid.ID and bid.Type=2 and bid.FirmID=".$userID."
					where copyright.FirmID=".$userID;
				break;
			case 4:
				$sql	.=	' where copyright.PractitionerID='.$userID;
				break;
		}
		
		
		return $this->findBySql($sql);
	}
	
	function findBidCopyrights($firmID)
	{
		$sql	=	"
			select distinct copyright.*,client.Business as ClientName,firm.Name as FirmName,
				inventorUser.Email as InventorEmail,inventorUser.FirstName as InventorFirstName,inventorUser.LastName as InventorLastName,
				practitionerUser.Email as PractitionerEmail,practitionerUser.FirstName as PractititonerFirstName,
				practitionerUser.LastName as PractitionerLastName,bid.Budget
				
				 from 
			
				copyright
							
				left join client on copyright.ClientID=client.ClientID 
							
				left join firm on copyright.FirmID=firm.FirmID
							
				left join inventor on copyright.InventorID=inventor.InventorID
							
				left join practitioner on copyright.PractitionerID=practitioner.PractitionerID
				
				left join user as inventorUser on inventorUser.UserID=inventor.UserID
				
				left join user as practitionerUser on practitionerUser.UserID=practitioner.UserID
		";

		$sql	.=	" left join bid  on copyright.CopyrightID=bid.ID and bid.Type=2 and bid.FirmID=".$firmID."
					where copyright.BBid=1 and copyright.FirmID=0 ";
		
		return $this->findBySql($sql);
	}
	
	function findCopyright($copyrightID,$userID,$role)
	{
		$sql	=	"
			select distinct copyright.*,client.Business as ClientName,firm.Name as FirmName,
				inventorUser.Email as InventorEmail,inventorUser.FirstName as InventorFirstName,inventorUser.LastName as InventorLastName,
				practitionerUser.Email as PractitionerEmail,practitionerUser.FirstName as PractititonerFirstName,
				practitionerUser.LastName as PractitionerLastName,bid.Budget
				
				 from 
			
				copyright
							
				left join client on copyright.ClientID=client.ClientID 
							
				left join firm on copyright.FirmID=firm.FirmID
							
				left join inventor on copyright.InventorID=inventor.InventorID
							
				left join practitioner on copyright.PractitionerID=practitioner.PractitionerID
				
				left join user as inventorUser on inventorUser.UserID=inventor.UserID
				
				left join user as practitionerUser on practitionerUser.UserID=practitioner.UserID
				
				
		";
		
		if($role == 3)
		{
			$sql	.=	"left join bid  on copyright.CopyrightID=bid.ID and bid.Type=2 and bid.FirmID=".$userID."
					where copyright.CopyrightID=".$copyrightID;
			
		}
		else
		{
			$sql	.=	"left join bid  on bid.ID=copyright.CopyrightID 
					where copyright.CopyrightID=".$copyrightID;
		}
		
		$copyrights	=	$this->findBySql($sql);
		return $copyrights[0];
	}
}
?>